unit TimeInteger;

interface

procedure IntegerToTime(Value: Cardinal; var Hour, Min, Sec: Cardinal);
function TimeToInteger(Hour, Min, Sec: Cardinal): Cardinal;
function TimeIntegerToString(Value: Cardinal; Hours: Boolean = false): String;
function TimeDiffToStr(DateTime: TDateTime): String;

implementation

uses
 SysUtils,
 DateUtils,
 StrUtils;

procedure IntegerToTime(Value: Cardinal; var Hour, Min, Sec: Cardinal);
begin
 if Value>0 then
  begin
   Sec:=Value mod 60;
   Min:=Value div 60;
   Hour:=0;
   while Min>59 do
    begin
     Inc(Hour);
     Dec(Min, 60);
    end;
  end
 else
  begin
   Hour:=0;
   Min:=0;
   Sec:=0;
  end;
end;

function TimeToInteger(Hour, Min, Sec: Cardinal): Cardinal;
begin
 Result:=Hour*3600+Min*60+Sec
end;

function TimeIntegerToString(Value: Cardinal; Hours: Boolean = false): String;
 var
  h, m, s: Cardinal;
  st: String;
 procedure AddZero(var AStr: String);
  begin
   while Length(AStr)<2 do
    AStr:='0'+AStr;
  end;
begin
 {
 if Value<0 then
   Result:=''
 else
 }
  begin
   IntegerToTime(Value, h, m, s);
   st:='';
   if Hours or (h>0) then
    begin
     st:=IntToStr(h);
     AddZero(st);
    end;
   Result:=IfThen(st='', '', st);
   st:=IntToStr(m);
   AddZero(st);
   Result:=IfThen(Result='', st, Result+':'+st);
   st:=IntToStr(s);
   AddZero(st);
   Result:=Result+':'+st;
  end;
end;

function TimeDiffToStr(DateTime: TDateTime): String;
begin
 Result:=TimeIntegerToString(SecondsBetween(Now, DateTime));
end;

end.
 